<?php
/**
 * Babel translation list
 * 
 * @package BABEL
 * @since   BABEL 0.2.0
 * @version $Id: profile.php 224 2008-03-05 09:07:06Z anders $
 * @copyright Copyright (C) 2008 Audun Skjelvan
 * @author  Audun Skjelvan
 * @licens  LGPL
 */

$obj_tpl->setFile("view_translations", "babel/view_translations.html");

$user_id = $_SESSION['user']['user_id'];

/*
 * SQL statement which returns the languages from the DB
 */
$DB_QUERY = "
			SELECT DISTINCT
				l.language_id AS id,
				l.name AS name
			FROM
				babel_languages l
			";

if (!PDODB::runQuery($DB_QUERY, null, $stmt)) {
	throw new Exception("Selecting language error");
}

/* {{{ Sets block */
$obj_tpl->setBlock("view_translations", "language_list", "language_list_ref");
/* }}} */

/* {{{ Sets info at the top of the dropdown list */
$obj_tpl->setVar(array(
						"ADD_LANG_ID"		=> "-1",
						"TXT_LANGUAGE"		=> "Please select language"
						)
				 );
$obj_tpl->parse("language_list_ref", "language_list",TRUE);
/* }}} */

/* {{{ Populates the dropdown list */
foreach($stmt as $row) {
	$obj_tpl->setVar(array(
		"ADD_LANG_ID"		=> $row["id"],
		"TXT_LANGUAGE"		=> $row["name"],)
	);
	$obj_tpl->parse("language_list_ref", "language_list",TRUE);
}
/* }}} */

/* {{{ If the user has chosen a valid language */
if(($_POST["lang_list"]) != -1){
	
	/*
	 * SQL statement which returns all the translations of a given language
	 *
	 * 1 param:
	 * $lang_list 	: 	The language id from the selected language
	 */
	$DB_QUERY = "
	SELECT 
		translation_id AS id,
		original_string AS original,
		translated_string AS translated,
		t.status AS status
	FROM
		babel_values v,
		babel_translations t
	WHERE
		v.value_id = t.value_id
	AND
		t.language_id = ?
	AND
		t.status NOT IN (?)
	ORDER BY
		original ASC
	";
	
if (!PDODB::runQuery($DB_QUERY, array($_POST["lang_list"], BABEL_TRANSLATE_STATUS_REJECTED), $stmt)) {
	throw new Exception("Selecting translations error");
}
/* }}} */
	
	$obj_tpl->setBlock("view_translations", "translations_list", "translations_list_ref");
		
	/* {{{ Populates the table with the translated text */
	$rowCount = 0;
	foreach($stmt as $row) {
		$obj_tpl->setVar(array(
			"ROW_TR_BGCOLOR"		=>	++$rowCount % 2 ? '#EEEEEE' : '#DDDDDD',
			"VAR_ORIGINAL_TEXT"		=> $row["original"],
			"VAR_TRANSLATED_TEXT"	=> $row["translated"],
			"VAR_TRANSLATION_ID"	=> $row["id"],
		));
		
		/* {{{ Chooses the proper status of the translation */ 
		switch($row["status"]) {
		case BABEL_TRANSLATE_STATUS_VOTE:
			$obj_tpl->setVar(array(
				"VAR_STATUS_TD_BGCOLOR"		=> VIEW_BABEL_TRANSLATE_STATUS_VOTE,
				"VAR_STATUS_TEXT"			=> "In voting",
				"VAR_STATUS_CELL_COLOR"		=> VIEW_BABEL_TRANSLATE_STATUS_APPROVED,
			));
			break;
		case BABEL_TRANSLATE_STATUS_APPROVED:
			$obj_tpl->setVar(array(
				"VAR_STATUS_TD_BGCOLOR"		=> VIEW_BABEL_TRANSLATE_STATUS_APPROVED,
				"VAR_STATUS_TEXT"			=> "Final",
				"VAR_STATUS_CELL_COLOR"		=> VIEW_BABEL_TRANSLATE_STATUS_APPROVED,
			));
			break;
		case BABEL_TRANSLATE_STATUS_SUPERSEDED:
			$obj_tpl->setVar(array(
				"VAR_STATUS_TD_BGCOLOR"		=> VIEW_BABEL_TRANSLATE_STATUS_SUPERSEDED,
				"VAR_STATUS_TEXT"			=> "Superseded",
				"VAR_STATUS_CELL_COLOR"		=> VIEW_BABEL_TRANSLATE_STATUS_APPROVED,
			));
			break;
		}
		/* }}} */
		
		$obj_tpl->parse("translations_list_ref", "translations_list",TRUE);
	}
	/* }}} */
/* }}} */
	
/* {{{ The user hasn`t chosen a language and an info message is shown. */
} else {
	$obj_tpl->setVar('VAR_INFOPANE',$obj_html->displayMessage("info", "In order to view any translations you have to choose a language."));
	$obj_tpl->parse("translations_list_ref", "translations_list",TRUE);
}
/* }}} */
$obj_tpl->pparse("view_translations", "view_translations", true);
?>
